﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SharpBase.Web
{
    /// <summary>
    /// Encapsulates a collection of Web Form parameters
    /// </summary>
    public class WebFormNameValuePairCollection : ICollection<WebFormNameValuePair>
    {
        Dictionary<string, WebFormNameValuePair> _Parameters;

        /// <summary>
        /// Initializes the WebFormNameValuePairCollection
        /// </summary>
        public WebFormNameValuePairCollection()
        {
            _Parameters = new Dictionary<string, WebFormNameValuePair>();
        }

        /// <summary>
        /// Adds a web form name/value pair to the collection
        /// </summary>
        /// <param name="item">Name/value pair to add</param>
        public void Add(WebFormNameValuePair item)
        {
            WebFormNameValuePair existing;
            if (_Parameters.TryGetValue(item.Name, out existing))
                existing.Value = item.Value;
            else
                _Parameters.Add(item.Name, item);
        }

        /// <summary>
        /// Clears all name/value pairs from the collection
        /// </summary>
        public void Clear()
        {
            _Parameters.Clear();
        }

        /// <summary>
        /// Checks whether the specified name/value pair is contained in the collection
        /// </summary>
        /// <param name="item">Name/value pair to check</param>
        /// <returns>Returns whether the name/value pair exists in the collection</returns>
        public bool Contains(WebFormNameValuePair item)
        {
            return _Parameters.ContainsValue(item);
        }

        /// <summary>
        /// Copies the name/value pair collection into the specified array at the specified starting index
        /// </summary>
        /// <param name="array">Array to copy into</param>
        /// <param name="arrayIndex">Starting index of the array to copy into</param>
        public void CopyTo(WebFormNameValuePair[] array, int arrayIndex)
        {
            int curIndex = arrayIndex;
            foreach(WebFormNameValuePair p in _Parameters.Values)
            {
                if (array.Length >= curIndex)
                    return;
                array[curIndex++] = p;
            }
        }

        /// <summary>
        /// Gets the number of elements in the collection
        /// </summary>
        public int Count
        {
            get { return _Parameters.Count; }
        }

        /// <summary>
        /// Gets whether the collection will accept new items
        /// </summary>
        public bool IsReadOnly
        {
            get { return false; }
        }

        /// <summary>
        /// Removes an item from the collection
        /// </summary>
        /// <param name="item">Name/value pair to remove from the collection</param>
        /// <returns>Returns whether the name/value pair was successfully removed</returns>
        public bool Remove(WebFormNameValuePair item)
        {
            return _Parameters.Remove(item.Name);
        }

        /// <summary>
        /// Returns a generic IEnumerator for enumerating through the collection
        /// </summary>
        /// <returns>Returns a generic IEnumerator for enumerating through the collection</returns>
        public IEnumerator<WebFormNameValuePair> GetEnumerator()
        {
            return _Parameters.Values.GetEnumerator();
        }

        /// <summary>
        /// Returns an IEnumerator for enumerating through the collection
        /// </summary>
        /// <returns>Returns an IEnumerator for enumerating through the collection</returns>
        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return _Parameters.Values.GetEnumerator();
        }
    }
}
